/*---- 倒计时 ----/

	使用示例 ( 10 分钟倒计时 ):
	var counter = new clsCounter ( 'counter' );
	counter.init ( 'divCounter', 600 , callback);

	参数说明: objId = 层ID, offsetTime = 剩余秒数, callback = 倒计时完成时回调函数(可缺省)

/------------------ */

function clsCounter ( clsName )
{
	this.clsName = clsName;
	this.expireTime = 0;
	this.interval = null;
	this.callback = null;
	this.delay = 1000;
	this.finishText = null; // 到时显示文字
	this.textDay = '%02s '; // 例: '%02s 天'
	this.textHour = '%02s:'; // 例: '%02s 时'
	this.textMinute = '%02s:'; // 例: '%02s 分'
	this.textSecond = '%02s'; // 例: '%02s 秒'
	
	this.textNoDetail = {
		1 : ' 1 分钟内 ',
		2 : ' 1 小时内 ',
		3 : ' 1 天内 '
		};

	this.showDetailDays = 0; // 小于该天数时开始显示 ( 0 表示无视该参数 )
	this.showLevel = 0; // 显示级别 ( 0: 秒, 1: 分, 2: 小时, 3: 天 )

	this.init = function ( objId, offsetTime, callback )
	{
		if(offsetTime <= 0) {
			if ( $( objId ) ) $( objId ).innerHTML = '<font  title="？: 后台程序未开启，或者正在忙" onclick="alert(\'后台程序未开启，或者正在忙\')" >00:00:0<a href="javascript:;" ><b><span style="color:#FFFF00;text-decoration: underline;padding-left:1px;">?</span></b></a></font>';
			return;
		}
		this.callback = callback;

		if ( this.interval == null )
		{
			offsetTime = Math.round ( offsetTime );
			var now = new Date ();
			this.expireTime = now.getTime () + offsetTime * 1000; // 记录到期时间
			this.update ( objId );
		}
	}

	this.update = function ( objId )
	{
		if ( obj = $ ( objId ) )
		{
			var now = new Date ();
			var offsetTime = Math.round ( ( this.expireTime - now.getTime () ) / 1000 );
			if ( offsetTime < 0 )
			{
				this.clear ();
				if ( this.callback ) this.callback ();
				if ( this.finishText ) obj.innerHTML = this.finishText;
				return;
			}

			offsetTime = Math.max ( 1, offsetTime );
			//days = Math.floor ( offsetTime / 86400 );
			//offsetTime -= days * 86400;
			hours = Math.floor ( offsetTime / 3600 );
			offsetTime -= hours * 3600;
			minutes = Math.floor ( offsetTime / 60 );
			offsetTime -= minutes * 60;
			seconds = offsetTime;

			//var showDays = this.showLevel > 3 ? '' : this.sprintf ( this.textDay, days );

			//var showDays = this.showLevel > 3 ? '' : this.sprintf ( this.textDay, days );
			if (hours < 100)
			{
				this.textHour = '%02s:'
			}
			else if (hours < 1000)
			{
				this.textHour = '%03s:'
			}
			else
			{
				this.textHour = '%04s:';
			}


			var showHours = this.showLevel > 2 ? '' : this.sprintf ( this.textHour, hours );
			var showMinutes = this.showLevel > 1 ? '' : this.sprintf ( this.textMinute, minutes );
			var showSeconds = this.showLevel > 0 ? '' : this.sprintf ( this.textSecond, seconds );
			//var showText = showDays + showHours + showMinutes + showSeconds;
			var showText =  showHours + showMinutes + showSeconds;

			if ( showText == '' && this.showLevel > 0 ) showText = this.textNoDetail[this.showLevel];

			if ( $( objId ) ) obj.innerHTML = showText;
			else {
				alert ('sadf');
				return false;
			}
		}

		this.interval = setTimeout ( this.clsName + ".update('" + objId + "')", this.delay );
	}

	this.sprintf = function ( format, data )
	{
		var re = /%([0-9]{1,2})s/g
		var r = format.match ( re );
		data = parseInt ( data );
		if ( isNaN ( data ) ) data = 0;

		if ( r != null )
		{
			var charFill = ' ';
			r = r.toString().replace ( re, "$1" );
			if ( r.length > 1 )
			{
				charFill = r.substr ( 0, 1 );
				r = r.substr ( 1, 1 );
			}

			var dataLength = data.toString().length;
			for ( i = dataLength; i < parseInt ( r ); i ++ )
			{
				data = charFill + data;
			}
			data = format.replace ( re, data );
			return data;
		}
		else if ( data > 0 )
		{
			data = data.toString() + format;
			return data;
		}
		else
		{
			return '';
		}
	}

	this.clear = function ()
	{
		if ( this.interval != null )
		{
			clearTimeout ( this.interval );
			this.interval = null;
		}
	}
}